﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Microsoft.Web.Mvc;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class ClientTypeController : Controller
    {
        //
        // GET: /ClientType/
        private readonly IClientTypeRepository clientTypeRepository;
        private readonly IUnitOfWork unitOfWork;
        public ClientTypeController(IClientTypeRepository clientTypeRepository, IUnitOfWork unitOfWork)
        {
            this.clientTypeRepository = clientTypeRepository;
            this.unitOfWork = unitOfWork;
        }
        private IList<ClientTypeInfoModel> GetClientType()
        {
            var clientType = clientTypeRepository.GetAll();
            IList<ClientTypeInfoModel> floorInfoModels = (from c in clientType
                                                      select new ClientTypeInfoModel
                                                     {
                                                         ID = c.ID,
                                                         Name = c.Name,
                                                         Discount = c.Discount,
                                                         Remark = c.Remark
                                                     }
                                                    ).ToList();
            return floorInfoModels;
        }

        public ActionResult ShowAllClientType()
        {
            return View(GetClientType());
        }

        [GridAction]
        public ActionResult TeletrikAjax()
        {
            return View(new GridModel(GetClientType()));
        }

        [HttpPost]
        [GridAction]
        public ActionResult AddClientType()
        {
            var clientTypeInfoModel = new ClientTypeInfoModel();
            if (TryUpdateModel(clientTypeInfoModel))
            {
                var clientType = new ClientType();
                ModelCopier.CopyModel(clientTypeInfoModel, clientType);
                clientTypeRepository.Add(clientType);
                unitOfWork.Commit();
            }
            return View(new GridModel(GetClientType()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult DeleteClientType(int Id)
        {
            var model = clientTypeRepository.Get(x => x.ID == Id);
            if (model != null)
            {
                clientTypeRepository.Delete(model);
                unitOfWork.Commit();
            }

            return View(new GridModel(GetClientType()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult UpdateClientType(int Id)
        {
            var model = clientTypeRepository.Get(x => x.ID == Id);
            if (TryUpdateModel(model))
            {
                clientTypeRepository.Update(model);
                unitOfWork.Commit();
            }

            return View(new GridModel(GetClientType()));
        }

    }
}
